//
//  ContainerBannerView.m
//  PaintWar
//
//  Created by Tai Le on 10/13/14.
//
//

#import "ContainerBannerView.h"
#import "UIViewAdditional.h"

#define MAC_HEI 90

@implementation ContainerBannerView

- (id)initWithBanner:(UIView *)banner showFromBottom:(BOOL)isFromBottom inParentView:(UIView *)parentView padding:(NSInteger)padding
{
    if (self = [super init])
    {
        self.backgroundColor = [UIColor blackColor];
        
        _statusBackgroundView = [[UIView alloc] init];
        _statusBackgroundView.backgroundColor = self.backgroundColor;
        [self addSubview:_statusBackgroundView];
        
        [self addSubview:banner];
        [parentView addSubview:self];
        
        _isShowFromBottom = isFromBottom;
        _padding = padding;
        
        [self initializeCloseButton];
        
        [self animateOut:banner parentHei:parentView.height];
    }
    
    return self;
}

- (void)initializeCloseButton
{
    _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _closeButton.size = CGSizeMake(34, 34);
    _closeButton.alpha = 0.8;
    [_closeButton addTarget:self action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [_closeButton setImage:[UIImage imageNamed:@"closeAdsButton.png"] forState:UIControlStateNormal];
    
    [self addSubview:_closeButton];
}

- (void)animateOut:(UIView *)banner parentHei:(NSInteger)parentHei
{
    
    NSInteger hei = banner.height;// + _closeButton.height;
    
    banner.bottom = hei;
    _closeButton.origin = CGPointMake(0, 0);
    
    //From bottom
    if (_isShowFromBottom)
    {
        self.frame = CGRectMake(0, parentHei + hei, banner.width, hei);
        _statusBackgroundView.height = 0;
        
        [UIView animateWithDuration:0.5f animations:^{
            self.bottom = parentHei - _padding;
        }];
    }
    //From Top
    else
    {
        self.frame = CGRectMake(0, -hei, banner.width, hei);
        _statusBackgroundView.frame = RECT(0, -20, self.width, 20);
        
        [UIView animateWithDuration:0.5f animations:^{
            self.top = _padding;
        }];
    }
}

- (void)animateIn
{
    //From bottom
    if (_isShowFromBottom)
    {
        [UIView animateWithDuration:0.5 animations:^{
            self.top = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
    //From Top
    else
    {
        [UIView animateWithDuration:0.5 animations:^{
            self.bottom = 0;
        } completion:^(BOOL finished) {
            if ([_delegate respondsToSelector:@selector(containerViewWillClose:)])
            {
                [_delegate containerViewWillClose:self];
            }
            
            [self removeFromSuperview];
        }];
        
    }
}

- (void)closeButtonPressed
{
    _closeButton.enabled = NO;
    [self animateIn];
}

@end
